package com.neiquan.neiquandemo.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.neiquan.neiquandemo.R;
import com.neiquan.neiquandemo.fmanager.StackManager;
import com.neiquan.neiquandemo.test.FourFragment_;
import com.neiquan.neiquandemo.test.OneFragment;
import com.neiquan.neiquandemo.test.ThreeFragment_;
import com.neiquan.neiquandemo.test.TwoFragment_;

import org.haitao.common.utils.AppLog;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * 作者 ： 邓勇军
 * 时间 ： 2016/12/29.
 * version:1.0
 */

public class MainActivity extends FragmentActivity {

    @InjectView(R.id.tab_home)
    LinearLayout home_lay;
    @InjectView(R.id.tab_talent)
    LinearLayout sort_lay;
    @InjectView(R.id.tab_how)
    RelativeLayout shop_lay;
    @InjectView(R.id.tab_me)
    LinearLayout mine_lay;
    private FrameLayout containerLL;
    public static StackManager manager;
    private int preId[] = { R.mipmap.tab_1_h, R.mipmap.tab_2_h, R.mipmap.tab_3_h, R.mipmap.tab_4_h };
    private int norId[] = { R.mipmap.tab_1_n, R.mipmap.tab_2_n, R.mipmap.tab_3_n, R.mipmap.tab_4_n };
    private ViewGroup Ly[];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);
        containerLL = ((FrameLayout) findViewById(R.id.framLayoutId));
        manager = new StackManager(this);
        manager.setFragment(new OneFragment());
        setFragmentAnim();
        Ly = new ViewGroup[]{home_lay, sort_lay, shop_lay, mine_lay};
    }

    @Override
    public final boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                manager.onBackPressed();
                return true;
            default:
                break;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void setFragmentAnim() {
        //设置fragment页面切换动画
//        manager.setAnim();
    }

    private int position=0;
    public void onTabClicked(View view) {
        switch (view.getId()) {
            case R.id.tab_home:
                position=0;
                manager.setFragment(new OneFragment());
                break;
            case R.id.tab_talent:
                position=1;
                manager.setFragment(new TwoFragment_());
                break;
            case R.id.tab_how:
                position=2;
                manager.setFragment(new ThreeFragment_());
                break;
            case R.id.tab_me:
                position=3;
                manager.setFragment(new FourFragment_());
                break;
            default:
                break;
        }
        setTabSelect(position);
    }


    private void setTabSelect(int position) {
        for (int i = 0; i < Ly.length; i++) {
            if (position == i) {
                ((ImageView) Ly[i].getChildAt(0)).setImageResource(preId[i]);
                ((TextView) Ly[i].getChildAt(1)).setTextColor(getResources().getColor(R.color.colorAccent));
            } else {
                ((ImageView) Ly[i].getChildAt(0)).setImageResource(norId[i]);
                ((TextView) Ly[i].getChildAt(1)).setTextColor(getResources().getColor(R.color.colorPrimary));
            }
        }
    }

    public void back(View view) {
        manager.onBackPressed();
    }
}
